package com.meetingshow.meeting.actions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;

import com.meetingshow.common.PageBean;
import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.common.base.uiservice.exception.BaseUIServiceException;
import com.meetingshow.meeting.jsonbean.MeetingJsonBean;
import com.meetingshow.meeting.jsonbean.TopicJsonBean;
import com.meetingshow.meeting.uiservice.IUIMeetingService;
import com.meetingshow.meeting.uiservice.IUITopicService;
import com.meetingshow.meeting.uiservice.IUIUserFollowService;
import com.meetingshow.meeting.uiservice.model.MeetingQueryParasVO;
import com.meetingshow.meeting.uiservice.model.MeetingVO;
import com.meetingshow.meeting.uiservice.model.TopicQueryParasVO;
import com.meetingshow.meeting.uiservice.model.TopicVO;
import com.meetingshow.meeting.uiservice.model.UserFollowQueryParasVO;
import com.meetingshow.meeting.uiservice.model.UserFollowVO;
import com.meetingshow.privilege.jsonbean.UserJsonBean;
import com.meetingshow.privilege.uiservice.model.UserVO;
import com.meetingshow.utils.JSONUtils;
import com.meetingshow.utils.UIServiceFactory;

/**
 * 手机调用访问
 * @author ivan
 *
 */
@Namespace("/met")
public class MeetingAction extends MeetingBaseAction {

    private String param;
    
    /**
     * 
     */
    private static final long serialVersionUID = 5480288936434299052L;

    private final static Logger logger = Logger.getLogger(MeetingAction.class);

    @Action(value = "detail")
    public String getMeeting() {
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            if (StringUtils.isBlank(param)) {
                response.getWriter()
                        .write(buildErrorJson("-1", "No parameter."));
                return null;
            }
            
            JSONObject jObj = JSONObject.fromObject(param);
            MeetingJsonBean mJsonBean = (MeetingJsonBean) JSONObject.toBean(jObj, MeetingJsonBean.class);
            
            if (StringUtils.isBlank(mJsonBean.getMeetId())) {
                response.getWriter().write(buildErrorJson("-1", "MeetingId cannot be empty."));
            }
            
            MeetingQueryParasVO meetingQueryParasVO = new MeetingQueryParasVO();
            meetingQueryParasVO.setMeetingId_e(mJsonBean.getMeetId());
            IUIMeetingService uiMeetingService = UIServiceFactory.getUIMeetingService();
            QueryResults<MeetingVO> mQueryResults = uiMeetingService.queryMeeting(meetingQueryParasVO);
            
            if (QueryResults.resultsListNotNull(mQueryResults)) {
                MeetingVO meetingVO = mQueryResults.getResultList().get(0);
                MeetingJsonBean returnJsonBean = JSONUtils.toMeetingJsonBean(meetingVO);
                response.getWriter().write(JSONObject.fromObject(returnJsonBean).toString());
            } else {
                response.getWriter().write(buildErrorJson("-1", "No meeting with id '" + mJsonBean.getMeetId() + "'."));
            }
            
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            try {
                if (e instanceof BaseUIServiceException) {
                    response.getWriter().write(buildErrorJson("-1", e.getMessage()));
                } else {
                    response.getWriter().write(buildErrorJson("-1", "GetMeeting error."));
                }
            } catch (IOException ex) {
                logger.error(ex.getMessage(), ex);
            }
        }
        return null;
    }

    /**
     * 手机调用查询推荐会议
     * @return
     */
    @Action(value = "getRecmd")
    public String getRecommend() {
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            List<MeetingVO> meetingList = getRecommendMeetings(String.valueOf(PageBean.SINGLE_PAGE_CUR_PAGE - 1), String.valueOf(PageBean.SINGLE_PAGE_ROWS_MAX)).getResultList();
            List<MeetingJsonBean> mJsonList = new ArrayList<MeetingJsonBean>();
            if (meetingList != null && meetingList.size() > 0) {
                for (MeetingVO meetingVO : meetingList) {
                    mJsonList.add(JSONUtils.toMeetingJsonBean(meetingVO));
                }
            }
            response.getWriter().write(JSONArray.fromObject(mJsonList).toString());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            try {
                if (e instanceof BaseUIServiceException) {
                    response.getWriter().write(buildErrorJson("-1", e.getMessage()));
                } else {
                    response.getWriter().write(buildErrorJson("-1", "GetRecommend error."));
                }
            } catch (IOException ex) {
                logger.error(ex.getMessage(), ex);
            }
        }
        return null;
    }
    
    @Action(value = "getUF")
    public String getUsersFollow() {
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            if (StringUtils.isBlank(param)) {
                response.getWriter()
                        .write(buildErrorJson("-1", "No parameter."));
                return null;
            }
            
            JSONObject jObj = JSONObject.fromObject(param);
            MeetingJsonBean mJsonBean = (MeetingJsonBean) JSONObject.toBean(
                    jObj, MeetingJsonBean.class);

            if (StringUtils.isBlank(mJsonBean.getMeetId())) {
                return null;
            }

            UserFollowQueryParasVO userFollowQueryParasVO = new UserFollowQueryParasVO();
            userFollowQueryParasVO.setMeetingId_e(mJsonBean.getMeetId());

            IUIUserFollowService uiUserFollowService = UIServiceFactory
                    .getUIUserFollowService();

            QueryResults<UserFollowVO> ufQueryResults = uiUserFollowService
                    .queryUserFollow(userFollowQueryParasVO);

            if (QueryResults.resultsListNotNull(ufQueryResults)) {
                List<UserJsonBean> userList = new ArrayList<UserJsonBean>();
                for (UserFollowVO userFollowVO : ufQueryResults.getResultList()) {
                    UserVO userVO = userFollowVO.getUser();
                    UserJsonBean uJsonBean = JSONUtils.toUserJsonBean(userVO);
                    userList.add(uJsonBean);
                }
                response.getWriter()
                        .write(JSONArray.fromObject(userList).toString());
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            try {
                if (e instanceof BaseUIServiceException) {
                    response.getWriter().write(buildErrorJson("-1", e.getMessage()));
                } else {
                    response.getWriter().write(buildErrorJson("-1", "GetUsersFollow error."));
                }
            } catch (IOException ex) {
                logger.error(ex.getMessage(), ex);
            }
        }
        return null;
    }

    @Action(value = "getT")
    public String getTopic() {
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            if (StringUtils.isBlank(param)) {
                response.getWriter()
                        .write(buildErrorJson("-1", "No parameter."));
                return null;
            }
            
            JSONObject jObj = JSONObject.fromObject(param);
            MeetingJsonBean mJsonBean = (MeetingJsonBean) JSONObject.toBean(
                    jObj, MeetingJsonBean.class);

            if (StringUtils.isBlank(mJsonBean.getMeetId())) {
                return null;
            }

            TopicQueryParasVO topicQueryParasVO = new TopicQueryParasVO();
            topicQueryParasVO.setMeetingId_e(mJsonBean.getMeetId());

            IUITopicService uiTopicService = UIServiceFactory
                    .getUITopicService();
            QueryResults<TopicVO> tQueryResults = uiTopicService
                    .queryTopic(topicQueryParasVO);

            if (QueryResults.resultsListNotNull(tQueryResults)) {
                List<TopicJsonBean> topicList = new ArrayList<TopicJsonBean>();
                for (TopicVO topicVO : tQueryResults.getResultList()) {
                    TopicJsonBean topicJsonBean = JSONUtils
                            .toTopicJsonBean(topicVO);
                    topicList.add(topicJsonBean);
                }
                response.getWriter()
                        .write(JSONArray.fromObject(topicList).toString());
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            try {
                if (e instanceof BaseUIServiceException) {
                    response.getWriter().write(buildErrorJson("-1", e.getMessage()));
                } else {
                    response.getWriter().write(buildErrorJson("-1", "GetTopic error."));
                }
            } catch (IOException ex) {
                logger.error(ex.getMessage(), ex);
            }
        }
        return null;
    }
    /**
     * @return the param
     */
    public String getParam() {
        return param;
    }

    /**
     * @param param
     *            the param to set
     */
    public void setParam(String param) {
        this.param = param;
    }
}
